﻿using System.Collections;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections.Generic;
using FreeBroker.Properties;

namespace FreeBroker
{
    [XmlRootAttribute("BrokerConfiguration", Namespace = "", IsNullable = false)]
    public class BrokerConfiguration
    {
        public BrokerConfiguration()
        {
        }

        /// <summary>
        ///     A list of all broker charts.
        /// </summary>
        [XmlArray("BrokerCharts"), XmlArrayItem("Broker", typeof(ChartOptions))]
        public ArrayList BrokerCharts = new ArrayList();

        public void AddBrokerCharts(List<ChartOptions> list)
        {
            foreach (ChartOptions item in list)
            {
                this.BrokerCharts.Add(item);
            }
        }

        /// <summary>
        ///     -1 means undefined, every other number is the chart options number.
        /// </summary>
        [XmlElement("StandardChartOptions")]
        public int StandardChartOptions = -1;

        /// <summary>
        ///     
        /// </summary>
        /// <returns>Standard chart options object or null if nothing is defined as standard.</returns>
        public ChartOptions GetStandardChartOptions(int newNumber)
        {
            if (StandardChartOptions == -1)
            {
                return null;
            }
            ChartOptions opt = GetChartOptionsByNumber(StandardChartOptions);
            if (opt != null)
            {
                ChartOptions clone = new ChartOptions();
                clone.Number = newNumber;
                clone.Options = (ArrayList)opt.Options.Clone();
                return clone;
            }
            return null;
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="number"></param>
        /// <returns>Found ChartOptions object or null if nothing was found.</returns>
        public ChartOptions GetChartOptionsByNumber(int number)
        {
            foreach (ChartOptions item in BrokerCharts)
            {
                if (item.Number == number)
                {
                    return item;
                }
            }
            return null;
        }
    }
}
